﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		EmailService.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 // 
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using Dexter.Core.EventArgs;
using Dexter.Domain.Model;
using Dexter.Logger.Contracts;
using Dexter.Repository;
using Dexter.Services;

namespace Dexter.Services.Implementation {
	/// <summary>
	/// 	The implementation of <see cref = "IEmailService" />.
	/// </summary>
	public class EmailService : IEmailService {
		/// <summary>
		/// 	An instance of a concrete implementation of <see cref = "IEmailRepository" />
		/// </summary>
		protected readonly IEmailRepository EmailRepository;

		/// <summary>
		/// 	The Logger
		/// </summary>
		protected readonly ILogger Logger;

		/// <summary>
		/// 	Initializes a new instance of the <see cref = "EmailService" /> class.
		/// </summary>
		/// <param name = "emailRepository">The email repository.</param>
		/// <param name = "logger">The logger.</param>
		public EmailService ( IEmailRepository emailRepository , ILogger logger ) {
			EmailRepository = emailRepository;
			Logger = logger;
		}

		/// <summary>
		/// 	Queues the specified email into dispatcher list.
		/// </summary>
		/// <param name = "item">The item.</param>
		public virtual void Queue ( EmailMessage item ) {
			var e = new CancelEventArgs ( );
			EmailQueueing.Raise ( this , e );

			if ( e.Cancel ) {
				return;
			}

			EmailRepository.Queue ( item );

			EmailQueued.Raise ( this , new GenericEventArgs <EmailMessage> ( item ) );
		}

		/// <summary>
		/// 	Peeks a set of <c>EmailMessage</c> from the queue.
		/// </summary>
		/// <param name = "size">The size of return's array.</param>
		/// <returns>
		/// 	An array of <c>EmailMessage</c>.
		/// 	The return object could be null if there isn't items into it.
		/// </returns>
		public virtual IEnumerable <EmailMessage> Peek ( int size ) {
			IEnumerable <EmailMessage> data = EmailRepository.Peek ( size );

			EmailsPeeked.Raise ( this , new GenericEventArgs <IEnumerable <EmailMessage>> ( data ) );

			return data;
		}

		/// <summary>
		/// 	The lifecycle of the item is completed, so this method removes it from the repository.
		/// </summary>
		/// <param name = "item">The item to remove.</param>
		public virtual void Dequeue ( EmailMessage item ) {
			var e = new CancelEventArgs ( );
			EmailQueueing.Raise ( this , e );

			if ( e.Cancel ) {
				return;
			}

			EmailRepository.Dequeue ( item );

			EmailQueued.Raise ( this , new GenericEventArgs <EmailMessage> ( item ) );
		}

		/// <summary>
		/// 	This event will raise when a <see cref = "EmailMessage" /> will be deleted by the implementation of <see cref = "Peek(int)" />.
		/// </summary>
		public virtual event EventHandler <GenericEventArgs <IEnumerable <EmailMessage>>> EmailsPeeked;

		/// <summary>
		/// 	This event will raise when a <see cref = "EmailMessage" /> will be deleted by the implementation of <see cref = "Queue(EmailMessage)" />.
		/// </summary>
		public virtual event EventHandler <GenericEventArgs <EmailMessage>> EmailQueued;

		/// <summary>
		/// 	This event will raise before a <see cref = "EmailMessage" /> will deleted by the implementation of <see cref = "Queue(EmailMessage)" />.
		/// </summary>
		public virtual event EventHandler <CancelEventArgs> EmailQueueing;

		/// <summary>
		/// 	This event will raise when a <see cref = "EmailMessage" /> will be deleted by the implementation of <see cref = "Dequeue(EmailMessage)" />.
		/// </summary>
		public virtual event EventHandler <GenericEventArgs <EmailMessage>> EmailDequeued;

		/// <summary>
		/// 	This event will raise before a <see cref = "EmailMessage" /> will deleted by the implementation of <see cref = "Dequeue(EmailMessage)" />.
		/// </summary>
		public virtual event EventHandler <CancelEventArgs> EmailDequeueing;
	}
}
